import java.util.Arrays;
import java.lang.String;
import java.util.*;

public class ContactCollection
{
        private Contact[] collection;
        private int count;
        private String password;
        
        //객체 배열 collection 생성
        public ContactCollection()
        {
                collection = new Contact[100];
                count = 0;
                password = "0000";
        }
                
        //배열 크기 증가
        private void increaseSize()
        {
                Contact[] temp = new Contact[collection.length * 2];
                for(int i=0; i<collection.length; i++)
                        temp[i] = collection[i];
                collection = temp;
        }
        
        //Get the password
        public String getPassword()
        {
                return password;
        }
        
        //Change the password
        public void changePassword(String pw)
        {
                password = pw;
                System.out.println("변경된 password는 " + password + "입니다");
        }

        //Add a new contact
        public void addContact(String na, String pos, String dep, String com, String add, String tel, String fa, String mob, String em, String home)
        {
                if(count == collection.length)
                        increaseSize();
                collection[count] = new Contact(na, pos, dep, com, add, tel, fa, mob, em, home);
                System.out.println("다음과 같이 연락처 등록이 완료되었습니다.\n" + collection[count].toString()); //추가된 연락처 출력
                count++;
        }
        
        //Display all contacts -> 이름, 연락처만 정렬해서 출력
        public void displayContact()
        {
              
        	String[] temp = new String [count];
            for(int i=0; i<count; i++)
            {
            	temp[i]=collection[i].getName();
            }
            
            Arrays.sort(temp);
            
            for(int i=0; i<count; i++)
            {
            	for(int j=0; j<count; j++)
            	{
            		if(temp[i].equals(collection[j].getName()))
            			System.out.println(collection[j].getName()+" "+collection[j].getMobile());
            	}
            }

                
        }
        
        //View a contact
        public void viewContact(String name)
        {
    		for(int i=0; i<count; i++)
    		{
    			if(name.equals(collection[i].getName()))
    				System.out.println(collection[i]);
    		}
        }
   
        //Search contacts -> 검색결과 이름만 출력
        public void searchContact(String name)
        {
                String result = "";
                for(int i=0; i<count; i++){
                        String getName = collection[i].getName();
                        if(getName.matches(".*" + name + ".*"))
                                result = collection[i].getName() + "\n";
                }
                System.out.println(result);
        }
        
        //Update contacts
        public void updateContact(String name, int updatenum, String updatedata)
        {
                for(int i=0; i<count; i++)
                {
                        if(name.equals(collection[i].getName()))
                        {
                                if(updatenum == 1)
                                {
                                        collection[i].setName(updatedata);
                                }
                                else if(updatenum == 2)
                                {
                                        collection[i].setPosition(updatedata);
                                }
                                else if(updatenum == 3)
                                {
                                        collection[i].setDepartment(updatedata);
                                }
                                else if(updatenum == 4)
                                {
                                        collection[i].setCompany(updatedata);
                                }
                                else if(updatenum == 5)
                                {
                                        collection[i].setAddress(updatedata);
                                }
                                else if(updatenum == 6)
                                {
                                        collection[i].setTelephone(updatedata);
                                }
                                else if(updatenum == 7)
                                {
                                        collection[i].setFax(updatedata);
                                }
                                else if(updatenum == 8)
                                {
                                        collection[i].setMobile(updatedata);
                                }
                                else if(updatenum == 9)
                                {
                                        collection[i].setEmail(updatedata);
                                }
                                else if(updatenum == 10)
                                {
                                        collection[i].setHomepage(updatedata);
                                }       
                        }
                }
                System.out.println("수정이 완료되었습니다!");
        }
        
        //Delete a contact 
        public void deleteContact(String name,String check)
        {
                if(check.compareTo("y") == 0)
                {
                for(int i=0; i<count; i++)
                        if(name.equals(collection[i].getName()))
                                for(;i<count;i++)
                                        collection[i] = collection[i+1];
                count--;
                System.out.println("삭제가 완료되었습니다!");
                }
                else if(check.compareTo("n") == 0)
                {
                        System.out.println("삭제가 취소되었습니다.");
                }
        }
}
